<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>设置查询条件</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongo.tutorial.cursor.html">使用游标获取所有文档</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongo.tutorial.multi.query.html">获取一个子集</a></div>
 <div class="up"><a href="mongo.tutorial.html">教程</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="mongo.tutorial.criteria" class="section">
   <h2 class="title">设置查询条件</h2>
   <p class="para">
    我们可以建立一个查询，然后传递给
    <span class="function"><a href="mongocollection.find.html" class="function">MongoCollection::find()</a></span> 方法来查询集合的一个子集。
    例如：我们要查询 <em>&quot;i&quot;</em> 字段等于 <em>71</em>
    的文档，我们可以使用：
   </p>
   <div class="example" id="mongo.tutorial.criteria-example">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$connection&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$collection&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$connection</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">database</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">collectionName</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$query&nbsp;</span><span style="color: #007700">=&nbsp;array(&nbsp;</span><span style="color: #DD0000">'i'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">71&nbsp;</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$cursor&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$collection</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">find</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$query&nbsp;</span><span style="color: #007700">);<br /><br />while&nbsp;(&nbsp;</span><span style="color: #0000BB">$cursor</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">hasNext</span><span style="color: #007700">()&nbsp;)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$cursor</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getNext</span><span style="color: #007700">()&nbsp;);<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(2) {
  [&quot;_id&quot;]=&gt;
  object(MongoId)#6 (0) {
  }
  [&quot;i&quot;]=&gt;
  int(71)
  [&quot;_ns&quot;]=&gt;
  &quot;testCollection&quot;
}
</pre></div>
    </div>
   </div>
  </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongo.tutorial.cursor.html">使用游标获取所有文档</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongo.tutorial.multi.query.html">获取一个子集</a></div>
 <div class="up"><a href="mongo.tutorial.html">教程</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
